package com.zion.tencent_dnspod_api.controller;

import com.tencentcloudapi.dnspod.v20210323.models.DescribeRecordListRequest;
import com.zion.tencent_dnspod_api.config.TencentConfig;
import com.zion.tencent_dnspod_api.entity.vo.DescribeDomainListVo;
import com.zion.tencent_dnspod_api.entity.vo.DescribeRecordListVo;
import com.zion.tencent_dnspod_api.service.DnsPodServiceImpl;
import io.netty.handler.codec.dns.DnsRecordType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author ZionChen
 * @Date 2024/1/1 22:27
 * @PackageName:com.zion.dnspod_update.controller
 * @ClassName: DnspodController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("/dnspod")
@Slf4j
public class DnspodController {
    @Resource
    private DnsPodServiceImpl dnsPodService;

    @Resource
    private TencentConfig tencentConfig;

    @RequestMapping(value = "/domain/list", method = RequestMethod.GET)
    public DescribeDomainListVo getConfigList() {
        log.info("调用接口获取域名列表");
        return dnsPodService.getDomainNameList();
    }

    @RequestMapping(value = "/record/list", method = RequestMethod.POST)
    public DescribeRecordListVo getRecordList(@RequestBody DescribeRecordListRequest params) {
        log.info("调用提供域名查询解析记录列表");
        return dnsPodService.getDomainNameResolutionRecordList(params);
    }

    /**
     * 更新代码
     * @param password 密码
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public void updateDns(@RequestParam("pwd") String password ) {
        log.info("调用更新域名解析记录");
        if("123456".equals(password)){
            String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            log.info("开始执行定时任务。当前时间={}",format);
            try{

                dnsPodService.updateDns(tencentConfig.getDomainName());
                log.info("执行定时任务完毕");
            }catch (Exception e){
                log.error("执行定时任务异常");
            }
        }
    }


}
